﻿using Colorful.Furniture.MpManagement;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SKIT.FlurlHttpClient.Wechat.Api;
using Volo.Abp;
using Volo.Abp.Modularity;

namespace Colorful.Furniture.MiniProgram;

/// <summary>
///  小程序模块
/// </summary>
public class WechatMiniProgramModule : AbpModule
{
    /// <summary>
    ///  配置服务
    /// </summary>
    /// <param name="context"></param>
    /// <exception cref="UserFriendlyException"></exception>
    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        var configuration = context.Services.GetConfiguration();

        var configurationSection = configuration.GetSection("WechatMiniProgram");

        if (!configurationSection.Exists())
            throw new UserFriendlyException("请在appsettings.json中配置WechatMiniProgram节点");

        Configure<WechatMiniProgramOptions>(configurationSection);

        context.Services.AddSingleton<IWechatMiniProgramExtensions, WechatMiniProgramExtensions>();

        var config = configurationSection.Get<WechatMiniProgramOptions>();

        var wechatApiOptions = new WechatApiClientOptions
        {
            AppId = config.AppId,
            AppSecret = config.AppSecret
        };

        context.Services.AddTransient(_ => new WechatApiClient(wechatApiOptions));
    }
}